from django.http.response import DjangoJSONEncoder, JsonResponse
from rest_framework.response import Response

import json

class OpDRFJSONEncoder(DjangoJSONEncoder):
	"""
	重写DjangoJSONEncoder
	(1)默认返回支持中文格式的json字符串
	"""

	def __init__(self, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False,indent=None, separators=None, default=None):
		super().__init__(skipkeys=skipkeys, ensure_ascii=False, check_circular=check_circular,allow_nan=allow_nan, \
						 sort_keys=sort_keys, indent=indent, separators=separators,default=default)


class SuccessResponse(Response):

	def __init__(self, data=None, code=200, msg="success"):
		self.srd_data = {
			"code": code,
			"msg": msg,
			"data": data,
			"status": True
		}
		super().__init__(self.srd_data)

	def __str__(self):
		return str(self.srd_data)


class ErrorResponse(Response):

	def __init__(self, data=None, code=201, msg="error"):
		self.erd_data = {
			"code": code,
			"msg": msg,
			"data": data,
			"status": False
		}
		super().__init__(self.erd_data)

	def __str__(self):
		return str(self.erd_data)


class SuccessJsonResponse(JsonResponse):

	def __init__(self, data=None, code=200, msg="success", encoder=DjangoJSONEncoder, safe=True, json_dumps_params=None, **kwargs):
		sjr_data = {
			"code": code,
			"msg": msg,
			"data": data,
			"status": True
		}
		super().__init__(sjr_data, encoder, safe, json_dumps_params, **kwargs)


class ErrorJsonResponse(JsonResponse):

	def __init__(self, data=None, code=201, msg="error", encoder=OpDRFJSONEncoder, safe=True, json_dumps_params=None, **kwargs):
		ejr_data = {
			"code": code,
			"msg": msg,
			"data": data,
			"status": False
		}
		super().__init__(ejr_data, encoder, safe, json_dumps_params, **kwargs)


def DictResponse(data=None, code=1003, msg="error", encoder=OpDRFJSONEncoder, safe=True, json_dumps_params=None, **kwargs):
	json_data = {
		"code": code,
		"msg": msg,
		"data": data,
		"status": False
	}
	return json_data
